package com.qqtech.qquan.user.service;

import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.circle.model.CircleTopic;
import com.qqtech.qquan.circle.service.CircleTopicService;
import com.qqtech.qquan.community.enums.CommunityMessageStatusDictEnum;
import com.qqtech.qquan.community.enums.CommunityMessageTypeDictEnum;
import com.qqtech.qquan.community.model.CommunityMessageVo;
import com.qqtech.qquan.community.service.CommunityMessageService;
import com.qqtech.qquan.user.dao.UserTopicDao;
import com.qqtech.qquan.user.model.UserTopic;
import com.qqtech.qquan.user.model.UserTopicVo;

@Service
public class UserTopicServiceImpl extends BaseServiceImpl<UserTopic> implements UserTopicService {
	public static final Logger log = LoggerFactory.getLogger(UserTopicServiceImpl.class);

	@Resource
	private UserTopicDao userTopicDao;

	@Resource
	private CommunityMessageService communityMessageService;
	
	@Resource
	private CircleTopicService circleTopicService;
	

	@Override
	protected BaseDao<UserTopic> getBaseDao() {
		return userTopicDao;
	}

	@Override
	public int addUserTopic(UserTopicVo userTopicVo) {
		Integer id = userTopicDao.insert(userTopicVo);
		if (null != userTopicVo.getCommunityId()) {
			createCommunityMessage(userTopicVo);
		}
		if(StringUtil.isNotBlank(userTopicVo.getCircleIds())){
			createCircleTopice(userTopicVo.getCreatorId(), userTopicVo.getContent(), userTopicVo.getImg(),userTopicVo.getCircleIds());
		}
		return id;
	}
	/**
	 * 同步社区大喇叭的
	 * @param creatorId
	 * @param content
	 * @param img
	 */
	private void createCommunityMessage(UserTopicVo userTopicVo) {
		CommunityMessageVo communityMessageVo = new CommunityMessageVo();
		communityMessageVo.setCommunityId(userTopicVo.getCommunityId());
		communityMessageVo.setPv(0);
		communityMessageVo.setStatus(CommunityMessageStatusDictEnum.NORMAL.getCode());
		communityMessageVo.setIsShow(YesNoEnum.YES.getCode());
		communityMessageVo.setCreatorId(userTopicVo.getCreatorId());
		communityMessageVo.setContent(userTopicVo.getContent());
		communityMessageVo.setImg(userTopicVo.getImg());
		communityMessageVo.setType(CommunityMessageTypeDictEnum.GOSSIP.getCode());
		communityMessageVo.setIsOfficial(YesNoEnum.NO.getCode());
		communityMessageVo.setIsTop(YesNoEnum.NO.getCode());
		communityMessageService.insert(communityMessageVo);

	}
	/**
	 * 同步圈子的
	 * @param creatorId
	 * @param content
	 * @param img
	 * @param circleIds
	 */
	private void createCircleTopice(Integer creatorId, String content, String img,String circleIds) {
       Iterator<String> it = Splitter.on(";").trimResults().omitEmptyStrings().split(circleIds).iterator();
       List<String> listIds=Lists.newArrayList(it);
       if(listIds.size()>0){
      	 for (String string : listIds) {
      		 CircleTopic topic = new CircleTopic();
   				topic.setCircleId(Integer.valueOf(string));
   				topic.setContent(content);
   				topic.setImg(img);
   				topic.setCreatorId(creatorId);
   				circleTopicService.addTopic(topic);
   			}
       }

	}

	@Override
	public void increasePv(Integer topicId) {
		userTopicDao.increasePv(topicId, 1);
		
	}

	@Override
	public void increaseComment(Integer topicId, int addCount) {
		userTopicDao.increaseComment(topicId, addCount);
	}

	@Override
	public void increasePraise(Integer topicId, int addCount) {
		userTopicDao.increasePraise(topicId, addCount);
		
	}
}